package com.example.bookshelf.data;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class DataReader {
    public void SaveBookManager(Context context, BookManager bookManager) {
        try {
            FileOutputStream fileOutputStream = context.openFileOutput("MyBookManager.dat", Context.MODE_PRIVATE);
            ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
            outputStream.writeObject(bookManager);
            outputStream.close();
            Log.i("data flow", "LoadBookManager: Save data success");
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("data flow", "LoadBookManager: Save data false"+e.toString());
        }
    }

    @NonNull
    public BookManager LoadBookManager(Context context) {
        BookManager bookManager = new BookManager();
        try {
            FileInputStream fileIn = context.openFileInput("MyBookManager.dat");
            ObjectInput objectIn = new ObjectInputStream(fileIn);
            bookManager = (BookManager) objectIn.readObject();
            objectIn.close();
            fileIn.close();
            Log.i("data flow", "LoadBookManager: Load data success");
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("data flow", "LoadBookManager: Load data false"+e.toString());

        }

        return bookManager;
    }

}
